package driver

// 腾讯云cos上传接口管理

import (
	"errors"
	"github.com/gin-gonic/gin"
	v1 "golang-sgg-daijia/api/v1"
	"golang-sgg-daijia/internal/handler"
	"golang-sgg-daijia/internal/service"
	"net/http"
)

type CosHandler struct {
	*handler.Handler
	cosService service.CosService
}

func NewCosHandler(
	handler *handler.Handler,
	cosService service.CosService,
) *CosHandler {
	return &CosHandler{
		Handler:    handler,
		cosService: cosService,
	}
}

// Upload 上传
func (h *CosHandler) Upload(ctx *gin.Context) {
	path, _ := ctx.GetPostForm("path")
	file, err := ctx.FormFile("file")
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	cosUploadVo, err := h.cosService.UploadFile(ctx, file, path)
	if err != nil {
		if errors.Is(err, v1.ErrImageAuditFail) {
			v1.HandleError(ctx, v1.AuditErr, v1.ErrImageAuditFail, nil)
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, cosUploadVo)
}
